<?php
/**
 * @filename ScheduleController.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2014-12-21 17:49:31
 * @version 1.0
 * @Description 排班
 */
class ScheduleController extends FrontController
{
    private $limit = 15;
    /**
     * 排班信息
     */
    public function actionIndex()
    {
        $p = $this->get('p', 1);
        $staff = new NetworkStaff();
        $criteria = new CDbCriteria();
        $criteria->addColumnCondition(array('network_id' => $this->network_id));
        $criteria->addColumnCondition(array('status' => 1));
        $total = $staff->count($criteria);
        
        $criteria->offset = ($p - 1) * $this->limit;
        $criteria->limit = $this->limit;
        $list = $staff->findAll($criteria);
        $page = new Page($total, $this->limit);  
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('list', $list);
        $this->smarty->display('schedule/index.tpl');
    }
    /**
     * 添加员工
     */
    public function actionAdd()
    {
        $staff_id = (int)$this->get('staff_id');
        if ($staff_id) 
        {
            $staff = new NetworkStaff();
            $info = $staff->findByPk($staff_id);        
            $this->smarty->assign('info', $info);
        }

        $this->smarty->display('schedule/add.tpl');
    }
    public function actionDoadd()
    {
        $staff = new NetworkStaff();
        $staff->network_id = $this->network_id;
        $staff->station = $this->post('station');
        $staff->staff_name = $this->post('staff_name');
        $staff->staff_identifier = $this->post('staff_identifier');
        $staff->photo = $this->post('photo');
        $staff->temporary = intval($this->post('temporary'));
        $staff->shift = intval($this->post('shift'));
        $staff->remark = $this->post('remark');
        if ($staff->shift) {
            $staff->new_station = $this->post('new_station');
            $staff->shift_time = $this->post('shift_time');
        }
        $staff->update_time = time();
        if ($staff->save()) {
            $this->jump('添加成功', '/webadmin/staff/list');
        } else {
            $this->jump('添加失败');
        }
    }
    /**
     * 编辑
     */
    public function actionDoedit()
    {
        $staff = new NetworkStaff();
        $staff_id = $this->post('staff_id');
        $data['network_id'] = $this->network_id;
        $data['station'] = $this->post('station');
        $data['staff_name'] = $this->post('staff_name');
        $data['staff_identifier'] = $this->post('staff_identifier');
        $data['photo'] = $this->post('photo');
        $data['temporary'] = intval($this->post('temporary'));
        $data['shift'] = intval($this->post('shift'));
        $data['remark'] = $this->post('remark');
        if ($data['shift']) {
            $data['new_station'] = $this->post('new_station');
            $data['shift_time'] = $this->post('shift_time');
        }
        $data['update_time'] = time();
        if ($staff->updateByPk($staff_id, $data)) {
            $this->jump('修改成功', '/schedule/index');
        } else {
            $this->jump('修改失败');
        }
    }
    /**
     * 删除
     */
    public function actionDel()
    {
        $staff_id = (int)$this->get('staff_id');
        $staff = new NetworkStaff();
        $res = $staff->updateByPk($staff_id, array('status' => 0));
        if($res){
            $this->_ajax(array('status' => 1));
        }else{
            $this->_ajax(array('status' => 0));
        }
    }
    /**
     * 排班
     */
    public function actionPlan()
    {
        $staff_id = intval($this->get('staff_id'));
        //员工排班情况
        if ($staff_id) 
        {
            $schedule = new NetworkStaffSchedule();
            $info = $schedule->getByStaff($staff_id);
            $this->smarty->assign('info', $info);
        }
        $this->smarty->assign('staff_id', $staff_id);
        echo $this->smarty->fetch('schedule/plan.tpl');
    }
    /**
     * 排班
     */
    public function actionDoplan()
    {
        $start = $this->post('start');
        $end = $this->post('end');
        $wicket = $this->post('wicket');
        $staff_id = $this->post('staff_id');

        for($i=1;$i<=7;$i++) 
        {
            $index = $i - 1;
            if (empty($start[$index]) || empty($end[$index]) || empty($wicket[$index])) 
            {
                continue;
            }
             $schedule = new NetworkStaffSchedule();
            if ($info = $schedule->check($staff_id, $i)) {
                if ($info['start_time']!=$start[$index] || $info['end_time']!=$end[$index] || $info['wicket']!=$wicket[$index]) 
                {
                   
                    $data['start_time'] = $start[$index];
                    $data['end_time'] = $end[$index];
                    $data['staff_id'] = $staff_id;
                    $data['week'] = $i;
                    $data['wicket'] = $wicket[$index];
                    $schedule->updateByPk($info['schedule_id'],$data);
                }
            } else {
                $schedule->start_time = $start[$index];
                $schedule->end_time = $end[$index];
                $schedule->staff_id = $staff_id;
                $schedule->week = $i;
                $schedule->wicket = $wicket[$index];
                $schedule->save();
            }
        }
        //$this->_ajax(array('status' => 1,'note' => '操作成功'));
    }
    /**
     * 排期详情
     */
    public function actionDetail()
    {
        $staff_id = intval($this->get('staff_id'));
        //员工信息
        $staff = new NetworkStaff();
        $staff_info = $staff->findByPk($staff_id)->attributes;
        $this->smarty->assign('staff_info', $staff_info);
        $schedule = new NetworkStaffSchedule();
        $info = $schedule->getByStaff($staff_id);
        $this->smarty->assign('info', $info); 
        echo $this->smarty->fetch('schedule/detail.tpl');
    }
}